using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace CodePlex.Resourceful.Examples
{
    public static class SilverlightStreamingExample
    {

        public static void Run() {


            // silverlight streaming account info
            string info = Environment.GetEnvironmentVariable("SILVERLIGHT_STREAMING");

            string accountID = info.Split(':')[0];
            string accountSecret = info.Split(':')[1];


            // create client (service requires https) - http trace to standard out
            Client client = new Client(new Uri("https://silverlight.services.live.com/"));
            client.Tracer.AddListener(Console.Out);

            // authentication
            client.SetBasicAuth("", accountID, accountSecret);  


            //GET serviceRoot/accountId?max-items=number&after=fileSetName 
            //    * Fetch a list of file sets contained in the account. If max-items= is present, 
            //    it limits the number of returned file sets. If after= is present, the list begins with the
            //    first set name greater than the given name. The list consists of an XML file in the form: 
            //<filesets>
            //<fileset name="file set name" is-application="true or false"/>
            //...
            //<filesets>

            // get current fileset list
            client.GetRepresentation(accountID);




            //POST serviceRoot/accountId/fileSetName 
            //* Add a file set to the account. The body of the post must be a zip file, mime type "application/zip". 
            //    It must contain a top-level file named "manifest.xml" If a file set with this name already exists, 
            //    the operation fails. 

            // add a new fileset
            string fileSetName = "fs" + DateTime.Now.Ticks;
            client.PostRepresentation(accountID + "/" + fileSetName, MediaType.ApplicationZip, CreateFilesetZip());


            // get fileset list (should contain new fileset)
            client.GetRepresentation(accountID);



            //GET serviceRoot/accountId/fileSetName 
            //    * Fetch a list of files contained in the file set. The list consists of an XML file in the form: 
            //<fileset>
            //    <file name="file name" length="length in bytes"/>
            //    ...
            //</fileset>

            // get list of files in fileset
            client.GetRepresentation(accountID + "/" + fileSetName);
           

            //GET serviceRoot/accountId/fileSetName/fileName 
            //    * Retrieve a single file from a file set. There is currently no support for downloading the file set as an archive. 

            // get the actual file contents of one of the files
            client.GetRepresentation(accountID + "/" + fileSetName+ "/content.txt");
           


            //DELETE serviceRoot/accountId/fileSetName 
            //    * Deletes a file set from the account. 

            // cleanup - delete the fileset
            client.DeleteRepresentation(accountID + "/" + fileSetName);



            // get fileset list (should not contain new fileset)
            client.GetRepresentation(accountID);


            Utils.Prompt();

        }





        private static Stream CreateFilesetZip()
        {


            Dictionary<string, Stream> contents = new Dictionary<string, Stream>();

            contents["content.txt"] = Utils.ToStream("this is some content");
            contents["morecontent.txt"] = Utils.ToStream("this is some additional content");
            contents["manifest.xml"] = Utils.ToStream(@"
<SilverlightApp>
<source>content.txt</source>
</SilverlightApp>
");

            MemoryStream stream = new MemoryStream();

            ZipOutputStream zos = new ZipOutputStream(stream);
            
            zos.SetLevel(5); // 0 [none] - 9 [highest]

            foreach (string content in contents.Keys)
            {
                zos.PutNextEntry(new ZipEntry(content));
                Utils.BufferedTransfer(contents[content], zos);
            }

            zos.Finish();
            

            stream.Position = 0;
            return stream;
        }
    }
}
